/**
 * 添加商品输入框模糊查询
 */
$('.product-count').on('keyup','input',function(e){
	var keyWords=$(this).val();
	var ul=$(this).siblings('ul');
	search(keyWords, ul);
});

/**
 * 添加商品输入框模糊查询
 */
$('.product-count').on('focus','input',function(e){
	var keyWords=$(this).val();
	var ul=$(this).siblings('ul');
	search(keyWords, ul);
});

/**
 * 点击相应商品之后获取相关库存情况
 */
$('.product-count').on('click','ul li',function(){
	var productId=$(this).attr('data-id');
	var unit=$(this).attr('data-unit');
	var productPriceId = $(this).children("span:first-child").attr("data-id");
	var productSpecId = $(this).children("span:last-child").attr("data-id");
	var li=$(this);
	var ul=$(this).parent('ul');
	var html="";
	var html1="";
	var count=0;
	$.ajax({
		type:"POST",
		url:"/diconet/warehouse/warehouseProduct/findProductInvertory",
		data:{"productId": productId, "productPriceId": productPriceId, "productSpecId": productSpecId},
		success:function(data){
			var warehouseProductList = data.rows;
			if(warehouseProductList.length > 0){
				for(var i=0;i<warehouseProductList.length;i++){
					var comment = warehouseProductList[i];
					html+="<p class='storage'\>"+comment.productId+"：<span>"+comment.quantity+"</span>"+unit+"</p>";
					count = parseFloat(count,2) + parseFloat(comment.quantity,2);
					html1+="<p data-warehouseId="+comment.warehouseId+" data-productPriceId="+productPriceId+" data-productId="+productId+" >"+
					"<span>"+comment.productId+"<i>(目前库存"+comment.quantity+unit+")</i></span>"+
					"<input type='text' value='0'/>"+
					"</p>";
				};
				$('.product-count').children('div').html(html);
				$('.warehouse').html(html1);
				$('.actualInvertory').children('span:nth-child(1)').html(count);
				$('.actualInvertory').children('span:nth-child(2)').html(unit);
				$('.product-count').children('input').val(li.children('span:nth-child(1)').html() + '   ' + li.children('span:nth-child(2)').html());
				$('.product-count').children('input').attr('data-id',li.attr('data-id'));
				ul.css('display','none');
			}else{
				$.ajax({
					type:"POST",
					url:"/diconet/warehouse/warehouse/list/data",
					data:{},
					success:function(data){
						var warehouseList = data.rows;
						if(warehouseList.length > 0){
							for(var i=0;i<warehouseList.length;i++){
								var comment = warehouseList[i];
								html+="<p class='storage'\>"+comment.warehouseName+"：<span>0</span>"+unit+"</p>";
								html1+="<p data-warehouseId="+comment.id+" data-productPriceId="+productPriceId+" data-productId="+productId+" >"+
								"<span>"+comment.warehouseName+"<i>(目前库存0"+unit+")</i></span>"+
								"<input type='text' value='0'/>"+
								"</p>";
							};
							$('.product-count').children('div').html(html);
							$('.warehouse').html(html1);
							$('.actualInvertory').children('span:nth-child(1)').html(count);
							$('.actualInvertory').children('span:nth-child(2)').html(unit);
							$('.product-count').children('input').val(li.children('span:nth-child(1)').html());
							$('.product-count').children('input').attr('data-id',li.attr('data-id'));
							ul.css('display','none');
						}
					},
					error: function(){
						layer.msg('库存查询失败', {icon: 5, time: 1000});
					}
				});
			}
		},
		error:function(){
			layer.msg('库存查询失败', {icon: 5, time: 1000});
		}
	})
});

/**
 * 保存入库操作
 */
$(".btn-block").on('click','.queren',function(){
	//提交信息  updateWarehouseProductPrice
	var warehouseProductPriceS=[];
	var status = 0;
	$('.warehouse p').each(function(){
		var warehouseProductPrice={};
		warehouseProductPrice.warehouseId = $(this).attr('data-warehouseId');
		warehouseProductPrice.productPriceId = $(this).attr('data-productPriceId');
		warehouseProductPrice.productId = $(this).attr('data-productId');
		if(!isPositiveInteger($(this).children('input').val())){
			layer.msg('请输入正确的入库数量', {icon: 2,time:1000});
			$(this).children('input').val(0);
			status = 1;
		}
		warehouseProductPrice.quantity = $(this).children('input').val();
		warehouseProductPriceS.push(warehouseProductPrice);
	});
	if(status==1){
		return false;
	}
	$.ajax({
		type:'post',
		url:'/diconet/warehouse/warehouseProduct/productStorage',
		data:{warehouseProductStrs: JSON.stringify(warehouseProductPriceS)},
		dataType : 'json' ,
		success:function(data){
			if(data.success == 'T'){
				layer.msg('商品入库成功', {icon: 1, time: 1000},function(){
					window.location.reload();
				});
			}else{
				layer.msg('保存失败', {icon: 5, time: 1000});
			}
		},
		error:function(){
			layer.msg('保存失败', {icon: 5, time: 1000});
		}
	});
});

/**
 * 根据商品编码或者名称查询对应商品，并添加到对应的ul中
 * @param keyWords
 * @param ul
 */
function search(keyWords, ul){
	var html="";
//	if($.trim(keyWords)!=""){
		$.ajax({
			type:"POST",
			url:"/diconet/product/product/findProductByKey",
			data:{"key":keyWords},
			success:function(data){
				var productList = data.rows;
				var commentContent = '';
				if(productList.length > 0) {
					ul.css('display','block');
					for(var i=0; i<productList.length; i++) {
						var comment = productList[i];
						commentContent+="<li data-unit="+comment.littleUnitId+" data-id="+comment.id+"><span data-id="+comment.categoryId+">"+comment.productName+"</span><span data-id="+comment.brandId+">"+comment.keyWord+"</span></li>"
					}
					ul.html(commentContent);
				}else{
					ul.css('display','none');
				}
			},
			error:function(data){
				layer.msg('查询商品信息加载失败', {icon: 5, time: 1000});
			}
		});
//	}else{
//		ul.css('display','none');
//	}
};

/**
 * 验证是否为正整数
 * @param s
 * @returns
 */
function isPositiveInteger(s){
	var re = /^[0-9]+$/ ;
	return re.test(s)
}